package com.cloudsigns.factorymanager2.produce.adapter.produce_team;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.cusenum.CommonEnum;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartDailyFeedbackBean;
import com.cloudsigns.factorymanager2.project.myinerface.ItemClickListener;

import java.util.ArrayList;
import java.util.List;

public class ProduceTeamOrderDailyFeedbackAdapter<T> extends BaseAdapter {
    private Context context;
    private List<T> datas = new ArrayList<T>();
    private LayoutInflater inflater;
    private ItemClickListener itemClickListener;
    private String TAG = "";

    private int visableType;// ProduceTeamOrderDailyFeedbackFragment 日反馈 异常回复与每日时间回复 ui区分
    public static int VISABLETYPE_TOP = 0;
    public static int VISABLETYPE_NORMAL = 1;

    public ProduceTeamOrderDailyFeedbackAdapter(Context context, int layoutType) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        visableType = layoutType;
    }

    public void setData(List<T> list) {
        if (null != list) {
            this.datas.clear();
            this.datas.addAll(list);
        }
    }

    public void setItemClick(ItemClickListener clickListener) {
        this.itemClickListener = clickListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public T getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_daily_feedbak_for_produce_team, null);
            holder.tv_type = convertView.findViewById(R.id.tv_type);
            holder.tv_status = convertView.findViewById(R.id.tv_status);
            holder.img_arrow_right = convertView.findViewById(R.id.img_arrow_right);
            holder.img_no_feeedback = convertView.findViewById(R.id.img_no_feeedback);
            holder.rl_item = convertView.findViewById(R.id.rl_item);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        WorkPartDailyFeedbackBean bean = (WorkPartDailyFeedbackBean) datas.get(position);
        holder.rl_item.setTag(bean);

        if (bean.isNoFeedBack()) {
            holder.tv_type.setText(context.getResources().getString(R.string.no_feedback));
            holder.img_no_feeedback.setVisibility(View.VISIBLE);
            holder.img_arrow_right.setVisibility(View.GONE);
        } else {
            holder.img_no_feeedback.setVisibility(View.GONE);
            holder.img_arrow_right.setVisibility(View.VISIBLE);

            CommonEnum.WorkPartType type = bean.getType();
            if (null != type) {
                if (type == CommonEnum.WorkPartType.NORMAL) {
                    holder.tv_type.setText(context.getResources().getString(R.string.normal_feedback));

                    String handlerAccountId = bean.getHandlerAccountId();
                    if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                        holder.tv_status.setText(context.getResources().getString(R.string.already_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
                    } else {
                        holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
                    }
                } else if (type == CommonEnum.WorkPartType.UNUSUAL) {

                    CommonEnum.FeedbackStatus feedbackStatus = bean.getFeedbackStatus();
                    if (null != feedbackStatus) {
                        if (feedbackStatus == CommonEnum.FeedbackStatus.WAIT_CONFIRM) {
                            holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
                        } else if (feedbackStatus == CommonEnum.FeedbackStatus.CONFIRM_FINISH) {
                            holder.tv_status.setText(context.getResources().getString(R.string.wait_reply));
                            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
                        } else if (feedbackStatus == CommonEnum.FeedbackStatus.WAIT_REPLY_CONFIRM) {
                            holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                            holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FFC000));
                        } else if (feedbackStatus == CommonEnum.FeedbackStatus.REPLY_CONFIRM_FINISH) {
                            holder.tv_status.setText(context.getResources().getString(R.string.already_confirmed));
                            holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
                        }
                    }

                    CommonEnum.WorkPartSubType subType = bean.getSubType();
                    if (null != subType) {
                        if (visableType == VISABLETYPE_NORMAL) {
                            holder.tv_type.setText(context.getResources().getString(R.string.unusual_feedback));
                        } else {
                            if (subType == CommonEnum.WorkPartSubType.PLAN_UNUSUAL) {
                                holder.tv_type.setText(context.getResources().getString(R.string.plan_unusual));
                            } else if (subType == CommonEnum.WorkPartSubType.PRODUCE_UNUSUAL) {
                                holder.tv_type.setText(context.getResources().getString(R.string.machin_unusual));
                            }
                        }
                    } else {
                        holder.tv_type.setText(context.getResources().getString(R.string.unusual_feedback));
                    }

                } else if (type == CommonEnum.WorkPartType.APPLY_ADJUST) {
                    holder.tv_type.setText(context.getResources().getString(R.string.application_adjustment));

                    String handlerAccountId = bean.getHandlerAccountId();
                    if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                        holder.tv_status.setText(context.getResources().getString(R.string.already_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
                    } else {
                        holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                    }

                } else if (type == CommonEnum.WorkPartType.APPLY_RETURN) {
                    holder.tv_type.setText(context.getResources().getString(R.string.application_rework));

                    String handlerAccountId = bean.getHandlerAccountId();
                    if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                        holder.tv_status.setText(context.getResources().getString(R.string.already_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
                    } else {
                        holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                    }

                } else if (type == CommonEnum.WorkPartType.APPLY_SCRAP) {
                    holder.tv_type.setText(context.getResources().getString(R.string.application_scrap));

                    String handlerAccountId = bean.getHandlerAccountId();
                    if (null != handlerAccountId && !TextUtils.isEmpty(handlerAccountId) && !handlerAccountId.equals("null")) {
                        holder.tv_status.setText(context.getResources().getString(R.string.already_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.color_2c9a2d));
                    } else {
                        holder.tv_status.setText(context.getResources().getString(R.string.wait_confirmed));
                        holder.tv_status.setTextColor(context.getResources().getColor(R.color.rgb_FCD505));
                    }
                }
            }
        }
        if (null != itemClickListener) {
            holder.rl_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    itemClickListener.itemClick(view, position);
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView tv_type;
        private TextView tv_status;
        private ImageView img_arrow_right;
        private ImageView img_no_feeedback;
        private View rl_item;
    }
}
